<?php

namespace App\Kuafu\WaiMai\Controller\Ext;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Order\Enum\CodeEnum;
use App\Kuafu\WaiMai\Service\BaseWaiMaiOrderService;
use App\Kuafu\WaiMai\Service\MtWaiMaiOrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class MtWaiMaiCallbackController extends BaseController
{
    private $orderService;
    private array $requestData;

    // 美团通用返回成功
    private function mtSuccess(): string
    {
        return '{"data":"ok"}';
    }

    private function prepare(Request $request, string $functionName)
    {
        $this->orderService = BaseWaiMaiOrderService::getIns(CodeEnum::SOURCE_TYPE_MT)->init();
        $this->requestData = $request->all();
        foreach ($this->requestData as &$v) {
            $v = urldecode($v);
        }

        Log::channel('waiMai')->info("MtWaiMai Callback {$functionName}:", $this->requestData);
    }

    // 推送订单
    public function pushOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        $this->orderService->createOrder();

        return $this->mtSuccess();
    }

    // 推送催单
    public function warnOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        return $this->mtSuccess();
    }

    /**
     * 隐私号降级通知
     * 隐私号功能出现故障无法拨通时，通过此接口进行通知。收到通知后可通过/order/batchPullPhoneNumber 接口拉取用户真实手机号。 每次收到隐私号降级通知，都需要重新调用拉取接口，获取最新降级的订单的用户真实手机号。
     */
    public function privacyDegrade(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        return $this->mtSuccess();
    }

    /**
     * 取消订单推送
     * @param Request $request
     */
    public function cancelOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        $this->orderService->cancelOrder();
        return $this->mtSuccess();
    }

    /**
     * 推送全额退款信息
     * @param Request $request
     */
    public function refundTotalOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        $this->orderService->refundTotalOrder();
        return $this->mtSuccess();
    }

    // 推送部分退款信息
    public function refundPartOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        $this->orderService->refundPartOrder();
        return $this->mtSuccess();
    }

    // 商家接单
    public function acceptOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        $this->orderService->acceptOrder();
        return $this->mtSuccess();
    }

    // 完成订单 - 用户点，完成
    public function completeOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        $this->orderService->completeOrder();
        return $this->mtSuccess();
    }

    // 配送订单
    public function deliveringOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        $this->orderService->deliveringOrder();
        return $this->mtSuccess();
    }

    // 结算推送
    public function billOrder(Request $request)
    {
        $this->prepare($request, __FUNCTION__);

        $this->orderService->billOrder();
        return $this->mtSuccess();
    }
}
